﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace IClient
{
    class globalData
    {

        private static uint _loginCount = 0;
        private static XElement _root = null;
        private static Dictionary<string, string> _data = new Dictionary<string, string>( );

        public static string getValue( string key )
        {
            string v = string.Empty;
            _data.TryGetValue( key, out v );
            return v;
        }
        public static void setValue(string key,string value )
        {
            _data[key] = value;
        }
        public const string conf_url = "config.xml";
        public static bool first_login
        {
            get
            {
                return _loginCount == 0;
            }
        }

        public static void save( )
        {
            if ( null != _root )
            {
                //刷新数据
                foreach( var item in _data )
                {
                    if ( _root.Element( item.Key ) == null )
                        _root.Add( new XElement( item.Key ) );
                    _root.Element( item.Key ).Value = item.Value.ToString( );
                }
                _root.Save( conf_url );
            }
        }

        public static void load( )
        {
            if ( !File.Exists( conf_url ) )
            {
                //默认服务器地址
                string ip = "47.103.5.113";
                string port = "5566";

                //创建xml对象
                _root = new XElement( "root",
                    new XElement( "ip", ip ),
                    new XElement( "port", port ),
                    new XElement( "sole_id", 0 ),
                    new XElement( "login_count", _loginCount )
                  );
            }
            else
            {
                //读取ip配置
                try
                {
                    //加载本地xml对象
                    _root = XElement.Load( conf_url );

                    //递归遍历xml树节点的属性和值 
                    Action<XElement> func = null;
                    func = ( XElement element ) =>
                    {
                        foreach ( var e in element.Elements( ) )
                        {
                            if ( e.HasElements )
                            {
                                func( e );
                            }
                            else
                            {
                                _data.Add( e.Name.ToString( ), e.Value );
                            }
                        }
                    };
                    func( _root );

                    //登录次数自增1
                    _loginCount = Convert.ToUInt32( _data["login_count"] ) + 1;
                    _data["login_count"] = _loginCount.ToString( );
                }
                catch ( Exception ex )
                {
                    Console.WriteLine( $"unknown error: {ex.StackTrace}" );
                }
            }


        }


    }
}
